/*
 * @Author: 李石
 * @Date: 2024-05-18 09:43:23
 * @LastEditors: lishi
 * @LastEditTime: 2024-05-24 14:33:26
 * @Description: 
 * Copyright (c) 2024 by ${lishi0105@163.com}, All Rights Reserved. 
 */
#ifndef _HTTPSERVER_HPP_
#define _HTTPSERVER_HPP_

#include <thread>
#include <string>
#include <iostream>
#include <functional>
#include <unordered_map>
#include "mongoose.h"
#include "HttpDataHandler.hpp"

using HttpHandler = std::function<void(struct mg_connection*, struct mg_http_message*)>;

class HttpServer : public std::thread{
public:
    HttpServer(int port);
    ~HttpServer() ;

    void Run() ;
    void Start() ;
    void Stop() ;
    void Handle(std::string uri, struct mg_connection* connection, struct mg_http_message*);

private:
    static void EventHandler(struct mg_connection* connection, int event, void* event_data);
    const int port_;
    std::atomic<bool> running_;
    struct mg_mgr mgr_;
    struct mg_connection* connection_;
    HttpDataHandler::Ptr data_handler_;
    std::unordered_map<std::string, HttpHandler> handlers_;
};

#endif // _HTTPSERVER_HPP_